import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { BusiProvider } from '../../providers/busi/busi';
import { Global } from '../../common/global';

/**
 * Generated class for the YwywPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: "page-ywyw",
  templateUrl: "ywyw.html"
})
export class YwywPage {
  //1 是普通商品 2 商家商品
  type: any = "3";
  productlist: any;
  productlistCopy:any;
  constructor(public navCtrl: NavController, public navParams: NavParams, public busi:BusiProvider) {}

  ionViewDidLoad() {
    console.log("ionViewDidLoad YwywPage");
  }

  ionViewDidEnter(){
    this.getProductList();
  }

  getProductList(){
    this.busi.getProduct(this.type).subscribe(
      data=>{
        this.productlist = data.products;
        this.productlistCopy = data.products;

      },
      error=>{

      }
    );
  }
  productdetail(prodid){
    this.navCtrl.push("ProductdetailPage",{prodid:prodid});
  }
  getImageUrl(url ){
    return Global.base_url + url;
  }

  search(env){
    var val = env.target.value;

    // if the value is an empty string don't filter the items
    if (val && val.trim() != '') {
      this.productlistCopy = this.productlist.filter((item) => {
        return (item.title.indexOf(val) > -1 || item.desc.indexOf(val) > -1);
      })
    }else{
      this.productlistCopy = this.productlist;
    }
  }
}
